package cn.cqautotest.cqviehelper.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;

import androidx.core.content.ContextCompat;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.vondear.rxtool.RxTool;
import com.youth.banner.transformer.MZScaleInTransformer;

import java.util.ArrayList;

import butterknife.BindView;
import cn.cqautotest.cqviehelper.R;
import cn.cqautotest.cqviehelper.base.BaseActivity;
import cn.cqautotest.cqviehelper.base.BaseFragment;
import cn.cqautotest.cqviehelper.base.BaseFragmentAdapter;
import cn.cqautotest.cqviehelper.config.Constants;
import cn.cqautotest.cqviehelper.ui.fragment.MeFragment;
import cn.cqautotest.cqviehelper.ui.fragment.SchoolCalendarFragment;
import cn.cqautotest.cqviehelper.ui.fragment.SchoolTimetableFragment;
import cn.cqautotest.cqviehelper.utils.ActivityManager;
import cn.cqautotest.cqviehelper.utils.NavigationMediator;
import cn.cqautotest.cqviehelper.utils.UpdateAppDataUtils;
import cn.cqautotest.cqviehelper.utils.ViewPager2Util;

/**
 * APP首页
 */
@SuppressLint({"NewApi", "NonConstantResourceId"})
public class HomeActivity extends BaseActivity {

    @BindView(R.id.mainViewPager2)
    ViewPager2 mViewPager;
    @BindView(R.id.mainBottomView)
    BottomNavigationView mainNavigation;
    private final ArrayList<BaseFragment<BaseActivity>> mFragments = new ArrayList<>();
    private BaseFragmentAdapter mAdapter;
    private NavigationMediator mMediator;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_home;
    }

    @Override
    protected void initView() {
        mAdapter = new BaseFragmentAdapter(this);
        mFragments.add(SchoolCalendarFragment.getInstance());
        //mFragments.add(AiChatFragment.getInstance());
        mFragments.add(SchoolTimetableFragment.getInstance());
        mFragments.add(MeFragment.getInstance());
        ViewPager2Util.changeToNeverMode(mViewPager);
        mViewPager.setUserInputEnabled(false);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setPageTransformer(new MZScaleInTransformer());
        mMediator = new NavigationMediator(mainNavigation, mViewPager, false);
        mMediator.attach();
        // 自定义对话框
        //new BaseDialog.Builder(this)
        //        .setContentView(R.layout.dialog_custom)
        //        //.setText(id, "我是预设置的文本")
        //        .setOnClickListener(R.id.btn_dialog_custom_ok, (BaseDialog.OnClickListener<ImageView>) (dialog, view) -> dialog.dismiss())
        //        .addOnShowListener(dialog -> {
        //            //ToastUtils.showShort("Dialog  显示了");
        //        })
        //        .addOnCancelListener(dialog ->{
        //            //ToastUtils.showShort("Dialog 取消了");
        //        })
        //        .addOnDismissListener(dialog -> {
        //            //ToastUtils.showShort("Dialog 销毁了");
        //        })
        //        .setOnKeyListener((dialog, event) -> {
        //            //ToastUtils.showShort("按键代码：" + event.getKeyCode());
        //            return false;
        //        }).show();
    }

    @Override
    protected void initData() {
        //销毁首页之外的Activity（放在子线程中执行）
        ActivityManager stackManager = ActivityManager.getInstance();
        RxTool.getBackgroundHandler().post(() -> stackManager.finishAllActivities(HomeActivity.class));
        //jumpToMake();
        mAdapter.setFragments(mFragments);
        //检查APP更新
        UpdateAppDataUtils.updateApp(this);
    }

    private int mPreIndex = 0;

    @Override
    protected void initEvent() {
        mViewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (position == 0 || position == 2) {
                    if (positionOffset > 3 / 4f) {
                        resetBgState();
                    }
                }
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                if (position != 2) {
                    mPreIndex = position;
                }
            }
        });
        mainNavigation.setOnNavigationItemSelectedListener(item -> {
            int index = mMediator.getIndexByItem(item);
            if (index == 0 || index == 2) resetBgState();
            mViewPager.setCurrentItem(index, false);
            return true;
        });
        //优化了底部导航栏长按时有提示的问题。
        Menu menu = mainNavigation.getMenu();
        for (int i = 0; i < menu.size(); i++) {
            MenuItem item = menu.getItem(i);
            mainNavigation.findViewById(item.getItemId()).setOnLongClickListener(v -> true);
        }
    }

    /**
     * 返回到上一个显示的界面
     */
    public void back2PreviousUI() {
        mViewPager.setCurrentItem(mPreIndex);
    }

    @Override
    protected void release() {
        mMediator.detach();
    }

    public void clearBgState() {
        mViewPager.setBackgroundColor(ContextCompat.getColor(this, R.color.transparent));
        mainNavigation.setBackgroundColor(ContextCompat.getColor(this, R.color.transparent));
    }

    public void resetBgState() {
        mViewPager.setBackgroundColor(ContextCompat.getColor(this, R.color.white));
        mainNavigation.setBackgroundColor(ContextCompat.getColor(this, R.color.white));
    }

    public ViewPager2 getViewPager() {
        return mViewPager;
    }

    public BottomNavigationView getMainNavigation() {
        return mainNavigation;
    }

    private void jumpToMake() {
        Intent intent = new Intent(this, WebViewActivity.class);
        intent.putExtra(Constants.STR_TITLE, "重庆工程职业技术学院 - By QQ：2695734816");
        intent.putExtra(Constants.STR_URL, Constants.MAKE_COMPLAINTS_URL);
        intent.putExtra(Constants.OPEN_ID, "cqvie_test_12346");
        intent.putExtra(Constants.NICK_NAME, "我是张三");
        intent.putExtra(Constants.HEADER_IMG_URL, "https://qlogo4.store.qq.com/qzone/2695734816/2695734816/100?0");
        startActivity(intent);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        checkIfReturn();
    }
}